package org.proteored.miapeapi.xml.mzidentml;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;

import org.apache.log4j.Logger;
import org.proteored.miapeapi.cv.ControlVocabularyManager;
import org.proteored.miapeapi.exceptions.IllegalMiapeArgumentException;
import org.proteored.miapeapi.exceptions.MiapeDatabaseException;
import org.proteored.miapeapi.exceptions.MiapeSecurityException;
import org.proteored.miapeapi.exceptions.WrongXMLFormatException;
import org.proteored.miapeapi.interfaces.msi.MiapeMSIDocument;
import org.proteored.miapeapi.interfaces.persistence.PersistenceManager;
import org.proteored.miapeapi.xml.mzidentml.autogenerated.MzIdentML;

public class MSIMiapeFactory {
	private static MSIMiapeFactory instance;
	private static Logger log = Logger.getLogger("log4j.logger.org.proteored");

	private MSIMiapeFactory() {
	}

	public static MSIMiapeFactory getFactory() {
		if (instance == null) {
			instance = new MSIMiapeFactory();
		}
		return instance;
	}

	public MiapeMSIDocument toDocument(MiapeMzIdentMLFile xmlFile,
			PersistenceManager databaseManager,
			ControlVocabularyManager cvManager, String userName,
			String password, String projectName, boolean processInParallel)
			throws MiapeDatabaseException, MiapeSecurityException,
			IllegalMiapeArgumentException {
		MiapeMSIDocument result = null;
		if (cvManager == null)
			throw new IllegalMiapeArgumentException(
					"ControlVocabularyManager is not set");
		try {
			log.info("before unmarshall");
			JAXBContext jc = JAXBContext
					.newInstance("org.proteored.miapeapi.xml.mzidentml.autogenerated");
			Unmarshaller unmarshaller = jc.createUnmarshaller();

			MzIdentML mzIdentML = (MzIdentML) unmarshaller.unmarshal(xmlFile
					.toFile());
			log.info("after unmarshall");
			if (databaseManager == null) {

				result = new MiapeMSIDocumentImpl(mzIdentML, cvManager, xmlFile
						.toFile().getName(), projectName, processInParallel);

			} else {
				result = new MiapeMSIDocumentImpl(mzIdentML, databaseManager,
						cvManager, userName, password, xmlFile.toFile()
								.getName(), projectName, processInParallel);

			}

		} catch (UnmarshalException e) {
			log.warn(e.getMessage());
			log.info("Input file format error. Check if the input file is mzIdentML schema compliant here: http://www-bs2.informatik.uni-tuebingen.de/services/OpenMS/analysisXML/index.php");
			throw new WrongXMLFormatException(
					"Input file format error. Check if the input file is mzIdentML schema compliant here: http://www-bs2.informatik.uni-tuebingen.de/services/OpenMS/analysisXML/index.php");
		} catch (JAXBException e) {
			e.printStackTrace();
			log.info(e.getMessage());
			throw new WrongXMLFormatException(e);
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			throw new WrongXMLFormatException(e);
		}

		return result;
	}

}
